#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
SDL_Window *window;
SDL_Renderer *render;
SDL_Texture *playerText;
int init()
{
    int ret = 1;
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("sdl could not init!sdl error:%s\n", SDL_GetError());
        ret = 0;
    }
    else
    {
        if ((window = SDL_CreateWindow("sdl", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 400, 300, 0)) == NULL)
        {
            printf("could not create! error:%s\n", SDL_GetError());
            ret = 0;
        }
        else
        {
            if ((render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED)) == NULL)
            {
                printf("render could not create! error:%s\n", SDL_GetError());
                ret = 0;
            }
            else
            {
                int imgFlags = IMG_INIT_PNG;
                if (!(IMG_Init(imgFlags) & imgFlags))
                {
                    printf("SDL_image could not initialize! SDL_image Error: %s\n",
                           IMG_GetError());
                    ret = 0;
                }
            }
        }
    }
    return ret;
}
SDL_Texture *loadTexture(char *path) {
  // The final texture
  SDL_Texture *newTexture = NULL;

  // Load image at specified path
  SDL_Surface *loadedSurface = IMG_Load(path);
  if (loadedSurface == NULL) {
    printf("Unable to load image %s! SDL_image Error: %s\n", path,
           IMG_GetError());
  } else {
    // Create texture from surface pixels
    newTexture = SDL_CreateTextureFromSurface(render, loadedSurface);
    if (newTexture == NULL) {
      printf("Unable to create texture from %s! SDL Error: %s\n", path,
             SDL_GetError());
    }

    // Get rid of old loaded surface
    SDL_FreeSurface(loadedSurface);
  }

  return newTexture;
}
void loadMedia()
{
  playerText = loadTexture("player.png");
}
int main(int argc, char *argv[])
{
    if (!init())
    {
        printf("init error!\n");
    }
    else
    {
        loadMedia();
        SDL_SetRenderDrawColor(render, 0, 0, 255, 255);
        SDL_RenderClear(render);
        SDL_Rect srcRect = {0,0,16,16};
        SDL_Rect dstRect = {100,100,16,16};
        SDL_RenderCopy(render, playerText,&srcRect,&dstRect);
    
        SDL_RenderPresent(render);
        SDL_Delay(4000);
         SDL_DestroyTexture(playerText);
        SDL_DestroyWindow(window);
        SDL_DestroyRenderer(render);
       
        SDL_Quit();
    }
    return 0;
}